from http import HTTPStatus
import dashscope
from dotenv import load_dotenv, find_dotenv
import os

load_dotenv()
dashscope.api_key = os.getenv('DASHSCOPE_API_KEY')


def qwen_util(user_content):
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': user_content}]
    try:
        response = dashscope.Generation.call(
            model='qwen-turbo',  # 确保此模型名称正确且可用
            messages=messages,
            result_format='message',  # 将返回结果格式设置为 message
        )
        if response.status_code == HTTPStatus.OK:
            print(response.output.choices[0])
            answer = response.output.choices[0].message.content
            return {'answer': answer}
        else:
            print('Request failed:')
            print(f"Request id: {response.request_id}, Status code: {response.status_code}, "
                  f"error code: {response.code}, error message: {response.message}")
            return {'status_code': response.status_code, 'message': response.message}
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return {'status_code': HTTPStatus.INTERNAL_SERVER_ERROR, 'message': 'An unexpected error occurred.'}


def generate_test_case_by_qwen(prompt):
    messages = [{"role": "system", "content": "你是一个专业的测试工程师，你需要根据用户的描述来生成测试用例,总结整个文档的，不用写测试脚本等其他内容。"}]
    messages.append({"role": "user", "content": prompt})
    print('qwen' + prompt)
    response = dashscope.Generation.call(
        model='qwen-max-longcontext',  # 确保此模型名称正确且可用
        messages=messages,
        result_format='message',  # 将返回结果格式设置为 message
        temperature=0
    )

    answer = response.output.choices[0].message.content + "\n\n" + "### * 追加：测试用例由AI生成，请自己斟酌后使用"
    return {'answer': answer}


if __name__ == '__main__':
    qwen_util('你好')
